package com.zzyl.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.BedDto;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.service.BedService;
import com.zzyl.vo.BedVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/bed")
@Api(tags = "床位管理相关接口")
public class BedController extends BaseController {

    @Autowired
    private BedService bedService;

    @GetMapping("/read/room/{roomId}")
    @ApiOperation(value = "根据房间id查询床位", notes = "传入房间id")
    public ResponseResult<List<BedVo>> readBedByRoomId(
            @ApiParam(value = "房间ID", required = true) @PathVariable("roomId") Long roomId) {
        List<BedVo> beds = bedService.getBedsByRoomId(roomId);
        return success(beds);
    }

    /**
     * 增加床位
     *
     * @param bedDto
     * @return
     */
    @PostMapping("/create")
    @ApiOperation("增加床位")
    @ApiImplicitParam(name = "bedDto" ,value="床位dto实体类",required = true,dataType = "BedDto")
    @ApiOperationSupport(includeParameters = {"bedDto.bedNumber","bedDto.roomId","bedDto.sort"})
    public ResponseResult create(@RequestBody BedDto bedDto) {
        try {
            log.info("增加的床位信息为:{}", bedDto);
            bedService.create(bedDto);
            return success();
        } catch (Exception e) {
            throw new BaseException(BasicEnum.BED_INSERT_FAIL);
        }
    }

    /**
     * 根据id查询床铺信息
     *
     * @param id
     * @return
     */
    @GetMapping("/read/{id}")
    @ApiOperation("查询床位")
    public ResponseResult readBed
    (@ApiParam(value = "床位ID",required = true)@PathVariable("id") Long id) {
        log.info("查询的id为:{}", id);
        BedVo bedVo = bedService.readBed(id);
        return success(bedVo);
    }

    /**
     * 更新床位
     *
     * @param bedDto
     * @return
     */
    @PutMapping("/update")
    @ApiOperation(value = "更新床位",notes = "传入床位对象,包括id,床位号.所属房间号等信息")
    public ResponseResult updateBed(@RequestBody BedDto bedDto) {
        log.info("修改的房间信息为:{}",bedDto);
        bedService.updateBed(bedDto);
        return success();
    }

    /**
     * 删除床位
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "删除床位",notes = "传入床位id")
    public ResponseResult deleteBed(@ApiParam(value = "床位ID",required = true ) @PathVariable("id") Long id) {
        log.info("删除的房间id为:{}",id);
        bedService.deleteBed(id);
        return success();
    }

}
